package com.shitou.pojo;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.shitou.util.DateUtil;

@Entity
@Table(name="t_user")
public class User {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	
	private String name;
	private String password;
	private String createdate;
	private String updatedate;
	private Boolean isenable;
	private int role;
	@ManyToMany
	@JoinTable(name="t_group_user",joinColumns=@JoinColumn(name="userid"),inverseJoinColumns=@JoinColumn(name="groupid"))
	private List<Group> groupList;
	
	@ManyToMany(mappedBy="users",fetch=FetchType.LAZY)
	private List<Project> projects;
	
	@OneToMany(mappedBy="user",fetch=FetchType.LAZY)
	private List<Issues> issueses;
	
	@OneToMany(mappedBy="user",fetch=FetchType.LAZY)
	private List<Assignment> assignments;
	
	//getter and setter
	public Set<String> getGroupNameSet() {
		Set<String> set = new HashSet<String>();
		for(Group g : groupList) {
			set.add(g.getName());
		}
		return set;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getCreatedate() {
		return createdate == null ? null : DateUtil.friendlyTime(DateUtil.stringToDate(createdate));
	}
	public void setCreatedate(String createdate) {
		this.createdate = createdate;
	}
	public String getUpdatedate() {
		return updatedate;
	}
	public void setUpdatedate(String updatedate) {
		this.updatedate = updatedate;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRole() {
		return role;
	}
	public void setRole(int role) {
		this.role = role;
	}

	public Boolean getIsenable() {
		return isenable;
	}
	public void setIsenable(Boolean isenable) {
		this.isenable = isenable;
	}
	public List<Project> getProjects() {
		return projects;
	}
	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}
	public List<Issues> getIssueses() {
		return issueses;
	}
	public void setIssueses(List<Issues> issueses) {
		this.issueses = issueses;
	}
	public List<Assignment> getAssignments() {
		return assignments;
	}
	public void setAssignments(List<Assignment> assignments) {
		this.assignments = assignments;
	}
	public List<Group> getGroupList() {
		return groupList;
	}
	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}
}
